/*******************************************************************************
 * Copyright (c) 2012 March and Conquest.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package mrusanov.towers.players;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import mrusanov.towers.cards.AbstractCard;
import mrusanov.towers.cards.CardList;
import mrusanov.towers.main.Game;
import mrusanov.towers.resources.PlayerIncome;
import mrusanov.towers.resources.PlayerResources;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

public class Player {

	private final static Random RANDOM = new SecureRandom();

	private final PlayerIncome income;
	private final PlayerResources resources;
	private final String name;
	private final List<AbstractCard> hand;
	private final Tower tower;

	public Player(String name) {
		super();
		this.name = name;
		income = new PlayerIncome();
		resources = new PlayerResources();
		hand = Lists.newArrayList();
		tower = new Tower();
	}

	public PlayerIncome getIncome() {
		return income;
	}

	public PlayerResources getResources() {
		return resources;
	}

	public String getName() {
		return name;
	}

	public List<AbstractCard> getHand() {
		return Collections.unmodifiableList(hand);
	}

	public Tower getTower() {
		return tower;
	}

	public void playCard(Game game, AbstractCard card) {
		Preconditions.checkArgument(hand.contains(card));
		hand.remove(card);

		card.executeAction(game, this, game.getEnemyPlayer());
		takeNewCard();
	}

	public void throwCard(AbstractCard card) {
		Preconditions.checkArgument(hand.contains(card));
		hand.remove(card);
		takeNewCard();
	}

	public void takeNewCard() {
		Class<? extends AbstractCard> cls = CardList.CARD_CLASSES.get(RANDOM.nextInt(CardList.CARD_CLASSES.size()));
		try {
			AbstractCard card = cls.newInstance();
			hand.add(card);
		} catch (Exception e) {
			// not likely to happen
			throw new AssertionError(e);
		}
	}

	public void addIncome() {
		getResources().addBeasts(getIncome().getBeasts());
		getResources().addBricks(getIncome().getBricks());
		getResources().addGems(getIncome().getGems());
	}

	@Override
	public String toString() {
		return name;
	}
}
